import conf from 'api/conf'
import axios from 'axios';
import qs from 'qs';
import storage from 'assets/js/storage'
import Vue from 'vue'

const io = {
    configUrls: function () {
// 登陆
        this.login = conf.baseApiPath + '/api/login'
        this.checkNeedCaptcha = conf.baseApiPath + '/api/captcha/checkNeedCaptcha'
        this.generCaptcha = conf.baseApiPath + '/api/captcha/generCaptcha'
        // 用户
        this.findUserPage = conf.baseApiPath + '/authUser/findUserPage'
        this.updateAuthUser = conf.baseApiPath + '/authUser/updateAuthUser'
        this.changeAuthUserStatus = conf.baseApiPath + '/authUser/changeAuthUserStatus'
        this.resetPass = conf.baseApiPath + '/api/resetPass'
        this.dataImport = conf.baseApiPath + '/api/word/dataImport'
        this.failedInfoExport = conf.baseApiPath + '/api/word/failedInfoExport'
        // 词典
        this.findWordRepoPage = conf.baseApiPath + '/api/wordRepo/findWordRepoPage'
        this.delWordRepo = conf.baseApiPath + '/api/wordRepo/delWordRepo'
        this.addWordRepo = conf.baseApiPath + '/api/wordRepo/addWordRepo'
        this.updateWordRepo = conf.baseApiPath + '/api/wordRepo/updateWordRepo'
        // 关卡
        this.findWordUnitPage = conf.baseApiPath + '/api/wordUnit/findWordUnitPage'
        this.delWordUnit = conf.baseApiPath + '/api/wordUnit/delWordUnit'
        this.addWordUnit = conf.baseApiPath + '/api/wordUnit/addWordUnit'
        this.updateWordUnit = conf.baseApiPath + '/api/wordUnit/updateWordUnit'
        // 单词
        this.findWordPage = conf.baseApiPath + '/api/word/findWordPage'
        this.delWord = conf.baseApiPath + '/api/word/delWord'
        this.addWord = conf.baseApiPath + '/api/word/addWord'
        this.updateWord = conf.baseApiPath + '/api/word/updateWord'
    },
    getHeaders: function () {
        const accessToken = storage.getAccessToken();
        return {
            accessToken
        }
    },
    post: async function (url, data, success, fail, timeout) {
        try {
            data = { ...data }
            let formdata = qs.stringify(data);
            let res = await axios({
                url: url,
                method: 'POST',
                data: formdata,
                dataType: 'json',
                timeout: timeout || 30000,
                headers:{accessToken: this.getHeaders().accessToken}
            });
            if (res.data.code === '3001') {
                storage.removeAccessToken()
                storage.removeCurrentUserInfo()
                // console.log('url',document.location)
                document.location.href="/"
                // Vue.prototype.$router.push('/login')
            }
            if (!res.data.success) {
                Vue.prototype.$notify.error({
                    title: '错误',
                    message: res.data.desc
                });
            }
            return res;
        } catch (err) {
            console.log(err)
            Vue.prototype.$notify.error({
                title: '错误',
                message: err.name
            })
        }
    },
    get: async function (url, data, success, fail, timeout) {
        try {
            data = { accessToken: this.getHeaders().accessToken, ...data }
            let formdata = qs.stringify(data);
            let res = await axios(url + "?" + formdata, {
                dataType: 'json',
                timeout: timeout || 30000
            });
            if (res.data.code === '3001') {
                storage.removeAccessToken()
                storage.removeCurrentUserInfo()
                Vue.prototype.$router.push('/login')
            }
            if (!res.data.success && !res.data.uptoken) {
                Vue.prototype.$notify.error({
                    title: '错误',
                    message: res.data.desc
                });
            }
            return res;
        } catch (err) {
            Vue.prototype.$notify.error({
                title: '错误',
                message: err.name
            })
            return
        }
    },

    getScripts: function (urls, done) {
        var $scripts = $.map(urls, function (url) {
            return $.cachedScript(url)
        });
        $scripts.push($.Deferred(function (deferred) {
            $(deferred.resolve);
        }));
        $.when.apply($, $scripts).done(done)
    },

}

io.configUrls()

export default io
